﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class TaskManagerUserDetailDA : ITaskManagerUserDetailDA
    {
        private string ALLOW_CASE = "@ALLOW_CASE";
        private string ALLOW_IPS = "@ALLOW_IPS";
        private string ALLOW_PALLET = "@ALLOW_PALLET";
        private string ALLOW_PIECE = "@ALLOW_PIECE";
        private string AREA_ID = "@AREA_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string LINE_ID = "@LINE_ID";
        private string PERMISSION = "@PERMISSION";
        private string PERMISSION_TYPE = "@PERMISSION_TYPE";
        private string SQL_CHECK_TASK_MANAGER_USER_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@TASK_MANAGER_USER_DETAIL WHERE USER_ID = @USER_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_TASK_MANAGER_USER_DETAIL = " DELETE FROM @_@TASK_MANAGER_USER_DETAIL WHERE USER_ID = @USER_ID ";
        private string SQL_INSERT_TASK_MANAGER_USER_DETAIL = " INSERT INTO @_@TASK_MANAGER_USER_DETAIL ( WH_ID, USER_ID, LINE_ID, PERMISSION_TYPE, AREA_ID, PERMISSION, DESCR, ALLOW_CASE, ALLOW_IPS, ALLOW_PALLET, ALLOW_PIECE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @USER_ID, @LINE_ID, @PERMISSION_TYPE, @AREA_ID, @PERMISSION, @DESCR, @ALLOW_CASE, @ALLOW_IPS, @ALLOW_PALLET, @ALLOW_PIECE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL = " SELECT  ROWID, WH_ID, USER_ID, LINE_ID, PERMISSION_TYPE, AREA_ID, PERMISSION, DESCR, ALLOW_CASE, ALLOW_IPS, ALLOW_PALLET, ALLOW_PIECE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@TASK_MANAGER_USER_DETAIL ";
        private string SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL_LANGUAGE = "SELECT  TMU.ROWID, TMU.WH_ID, TMU.USER_ID, TMU.LINE_ID,  T.[TEXT] AS PERMISSION_TYPE, TMU.AREA_ID, TMU.PERMISSION, TMU.DESCR, TMU.ALLOW_CASE, TMU.ALLOW_IPS, TMU.ALLOW_PALLET, TMU.ALLOW_PIECE, TMU.CREATED_BY, TMU.CREATED_DATE, TMU.UPDATED_BY, TMU.UPDATED_DATE  FROM  @_@TASK_MANAGER_USER_DETAIL TMU LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = TMU.PERMISSION_TYPE AND SCD.SYS_CODE_TYPE = 'TASKTYPE' LEFT JOIN SYSADM.TRANSLATION T ON 'TASKTYPE.'+TMU.PERMISSION_TYPE = T.ITEM_ID  AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = '@LANGUAGE'";
        private string SQL_UPDATE_TASK_MANAGER_USER_DETAIL = " UPDATE @_@TASK_MANAGER_USER_DETAIL SET WH_ID = @WH_ID, PERMISSION_TYPE = @PERMISSION_TYPE, AREA_ID = @AREA_ID, PERMISSION = @PERMISSION, DESCR = @DESCR, ALLOW_CASE = @ALLOW_CASE, ALLOW_IPS = @ALLOW_IPS, ALLOW_PALLET = @ALLOW_PALLET, ALLOW_PIECE = @ALLOW_PIECE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE USER_ID = @USER_ID AND LINE_ID = @LINE_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string USER_ID = "@USER_ID";
        private string WH_ID = "@WH_ID";

        public bool CheckTaskManagerUserDetailIDUnique(string userID, string LineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID), new SqlParameter("@LINE_ID", LineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_TASK_MANAGER_USER_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckTaskManagerUserDetailIDUnique(DataBase dataBase, DbTransaction tran, string userID, string LineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID), new SqlParameter("@LINE_ID", LineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_TASK_MANAGER_USER_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteTaskManagerUserDetail(string userID, List<string> LineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string sql = this.SQL_DELETE_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID) + "AND LINE_ID = @LINE_ID ";
            try
            {
                for (int j = 0; j < LineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID), new SqlParameter("@LINE_ID", LineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteTaskManagerUserDetail(DataBase dataBase, DbTransaction tran, List<string> userID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < userID.Count; i++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID[i]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteTaskManagerUserDetail(DataBase dataBase, DbTransaction tran, string userID, List<string> LineID, string whLoginID)
        {
            string sql = this.SQL_DELETE_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID) + "AND LINE_ID = @LINE_ID ";
            int result = 0;
            try
            {
                for (int j = 0; j < LineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID), new SqlParameter("@LINE_ID", LineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllTaskManagerUserDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllTaskManagerUserDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public TaskManagerUserDetailInfo GetTaskManagerUserDetailByID(string userID, string LineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID) + " WHERE USER_ID = @USER_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID), new SqlParameter("LINE_ID", LineID) };
            TaskManagerUserDetailInfo taskManagerUserDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    taskManagerUserDetailInfo = new TaskManagerUserDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["USER_ID"].ToString(), reader["LINE_ID"].ToString(), reader["PERMISSION_TYPE"].ToString(), reader["AREA_ID"].ToString(), reader["PERMISSION"].ToString(), reader["DESCR"].ToString(), reader["ALLOW_CASE"].ToString(), reader["ALLOW_IPS"].ToString(), reader["ALLOW_PALLET"].ToString(), reader["ALLOW_PIECE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return taskManagerUserDetailInfo;
        }

        public List<TaskManagerUserDetailInfo> GetTaskManagerUserDetailByID(string userID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL_LANGUAGE.Replace("@_@", whLoginID).Replace("@LANGUAGE", pubParasInfo.UserLanguage) + " WHERE USER_ID = @USER_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID) };
            List<TaskManagerUserDetailInfo> taskManagerUserDetailList = new List<TaskManagerUserDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    TaskManagerUserDetailInfo taskManagerUserDetailInfo = new TaskManagerUserDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["USER_ID"].ToString(), reader["LINE_ID"].ToString(), reader["PERMISSION_TYPE"].ToString(), reader["AREA_ID"].ToString(), reader["PERMISSION"].ToString(), reader["DESCR"].ToString(), reader["ALLOW_CASE"].ToString(), reader["ALLOW_IPS"].ToString(), reader["ALLOW_PALLET"].ToString(), reader["ALLOW_PIECE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    taskManagerUserDetailList.Add(taskManagerUserDetailInfo);
                }
            }
            return taskManagerUserDetailList;
        }

        public TaskManagerUserDetailInfo GetTaskManagerUserDetailByID(DataBase dataBase, DbTransaction tran, string userID, string LineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID) + " WHERE USER_ID = @USER_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@USER_ID", userID), new SqlParameter("LINE_ID", LineID) };
            TaskManagerUserDetailInfo taskManagerUserDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                taskManagerUserDetailInfo = new TaskManagerUserDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["USER_ID"].ToString(), reader["LINE_ID"].ToString(), reader["PERMISSION_TYPE"].ToString(), reader["AREA_ID"].ToString(), reader["PERMISSION"].ToString(), reader["DESCR"].ToString(), reader["ALLOW_CASE"].ToString(), reader["ALLOW_IPS"].ToString(), reader["ALLOW_PALLET"].ToString(), reader["ALLOW_PIECE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return taskManagerUserDetailInfo;
        }

        public DataSet GetTaskManagerUserDetailByQueryList(List<string> sqlWhere, TaskManagerUserDetailQueryEntity taskmanageruserdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!taskmanageruserdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, taskmanageruserdetailQuery.CurrentPage, taskmanageruserdetailQuery.PageSize, taskmanageruserdetailQuery.SortField, taskmanageruserdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetTaskManagerUserDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, TaskManagerUserDetailQueryEntity taskmanageruserdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, taskmanageruserdetailQuery.CurrentPage, taskmanageruserdetailQuery.PageSize, taskmanageruserdetailQuery.SortField, taskmanageruserdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertTaskManagerUserDetail(TaskManagerUserDetailInfo taskManagerUserDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                taskManagerUserDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUserDetail_Parameters(taskManagerUserDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertTaskManagerUserDetail(DataBase dataBase, DbTransaction tran, TaskManagerUserDetailInfo taskManagerUserDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                taskManagerUserDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUserDetail_Parameters(taskManagerUserDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_TaskManagerUserDetail_Parameters(TaskManagerUserDetailInfo taskManagerUserDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.USER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.VarChar, 5), new SqlParameter(this.PERMISSION_TYPE, SqlDbType.VarChar, 10), new SqlParameter(this.AREA_ID, SqlDbType.VarChar, 10), new SqlParameter(this.PERMISSION, SqlDbType.VarChar, 10), new SqlParameter(this.DESCR, SqlDbType.VarChar, 60), new SqlParameter(this.ALLOW_CASE, SqlDbType.VarChar, 1), new SqlParameter(this.ALLOW_IPS, SqlDbType.VarChar, 1), new SqlParameter(this.ALLOW_PALLET, SqlDbType.VarChar, 1), new SqlParameter(this.ALLOW_PIECE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 0x12), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 0x12) };
            if (!string.IsNullOrEmpty(taskManagerUserDetailInfo.WhID))
            {
                paramArray[0].Value = taskManagerUserDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = taskManagerUserDetailInfo.UserID;
            paramArray[2].Value = taskManagerUserDetailInfo.LineID;
            paramArray[3].Value = taskManagerUserDetailInfo.PermissionType;
            paramArray[4].Value = taskManagerUserDetailInfo.AreaID;
            paramArray[5].Value = taskManagerUserDetailInfo.Permission;
            paramArray[6].Value = taskManagerUserDetailInfo.Descr;
            if (!string.IsNullOrEmpty(taskManagerUserDetailInfo.AllowCase))
            {
                paramArray[7].Value = taskManagerUserDetailInfo.AllowCase;
            }
            else
            {
                paramArray[7].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(taskManagerUserDetailInfo.AllowIps))
            {
                paramArray[8].Value = taskManagerUserDetailInfo.AllowIps;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(taskManagerUserDetailInfo.AllowPallet))
            {
                paramArray[9].Value = taskManagerUserDetailInfo.AllowPallet;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(taskManagerUserDetailInfo.AllowPiece))
            {
                paramArray[10].Value = taskManagerUserDetailInfo.AllowPiece;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            paramArray[11].Value = taskManagerUserDetailInfo.CreatedBy;
            paramArray[12].Value = taskManagerUserDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateTaskManagerUserDetail(TaskManagerUserDetailInfo taskManagerUserDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                taskManagerUserDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUserDetail_Parameters(taskManagerUserDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateTaskManagerUserDetail(DataBase dataBase, DbTransaction tran, TaskManagerUserDetailInfo taskManagerUserDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                taskManagerUserDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_TaskManagerUserDetail_Parameters(taskManagerUserDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_TASK_MANAGER_USER_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

